package com.ruibang.glass.purchase.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.purchase.domain.req.PurchaseContractsReq;
import com.ruibang.glass.purchase.entity.PurchaseContracts;
import com.ruibang.glass.purchase.service.PurchaseContractsService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author mameng
 * 采购合同
 */
@Api(tags = "采购合同")
@RestController
@RequestMapping("/api/glass/purchase/contracts")
public class PurchaseContractsController {


    @Autowired
    private PurchaseContractsService purchaseContractsService;

    @PostMapping("/list")
    @ApiOperation(value = "采购合同列表", response = ResultBody.class)
    public ResultBody queryAreas(@RequestBody PurchaseContractsReq purchaseReportReq) {
        return ResultBody.success(purchaseContractsService.getPage(purchaseReportReq));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加合同", response = ResultBody.class)
    public ResultBody add(@RequestBody PurchaseContracts gPurchaseContracts) {
        purchaseContractsService.addPurchaseContracts(gPurchaseContracts);
        return ResultBody.success();
    }

    @PostMapping("/add/qz")
    @ApiOperation(value = "添加签章合同", response = ResultBody.class)
    public ResultBody addSignature(@RequestBody PurchaseContracts purchaseContracts) {
        purchaseContractsService.addSignature(purchaseContracts);
        return ResultBody.success();
    }

    @PostMapping("/processStart")
    @ApiOperation(value = "发起流程", response = ResultBody.class)
    public ResultBody processStart(@RequestBody BpmProcessInstanceCreate BpmProcessInstanceCreate) {
        purchaseContractsService.processStart(BpmProcessInstanceCreate);
        return ResultBody.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "合同修改", response = ResultBody.class)
    public ResultBody edit(@RequestBody PurchaseContracts gPurchaseContracts) {
        purchaseContractsService.updateGPurchaseContracts(gPurchaseContracts);
        return ResultBody.success();
    }

    @PostMapping("/addAbolishFile")
    @ApiOperation(value = "保存废止文件", response = ResultBody.class)
    public ResultBody addAbolishFile(@RequestBody PurchaseContracts purchaseContracts) {
        purchaseContractsService.addAbolishFile(purchaseContracts);
        return ResultBody.success();
    }

    @GetMapping("/processNotice/{businessId}")
    @ApiOperation(value = "流程处理通知", response = ResultBody.class)
    public ResultBody processNotice(@PathVariable("businessId") String businessId) {
        purchaseContractsService.processNotice(businessId);
        return ResultBody.success();
    }
    @GetMapping("/getOne/{id}")
    @ApiOperation(value = "获取详情", response = ResultBody.class)
    public ResultBody getOne(@PathVariable("id") String id) {
        return ResultBody.success(purchaseContractsService.getOneById(id));
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "详情", response = ResultBody.class)
    public ResultBody getById(@PathVariable("id") String id) {
        return ResultBody.success(purchaseContractsService.getOneById(id));
    }

    @PostMapping("/getChildNumber")
    @ApiOperation(value = "详情", response = ResultBody.class)
    public ResultBody getChildNumber(@RequestBody PurchaseContracts purchaseContracts) {
        return ResultBody.success(purchaseContractsService.getChildNumber(purchaseContracts));
    }

    @PostMapping("/removeChoose")
    @ApiOperation(value = "选中删除", response = ResultBody.class)
    public ResultBody removeOne(@RequestBody List<String> ids) {
        purchaseContractsService.batchDelete(ids);
        return ResultBody.success();
    }

    @GetMapping("/getContractsList")
    @ApiOperation(value = "获取采购合同列表", response = ResultBody.class)
    public ResultBody getContractsList() {
        return ResultBody.success(purchaseContractsService.list(Wrappers.<PurchaseContracts>lambdaQuery()
                .select(PurchaseContracts::getId,
                        PurchaseContracts::getContractName,
                        PurchaseContracts::getContractType,
                        PurchaseContracts::getSupplierId,
                        PurchaseContracts::getSupplierName)
                .eq(PurchaseContracts::getApproveStatus,CommonConstant.TWO_STR)
                .eq(PurchaseContracts::getDeleteStatus, CommonConstant.ONE_STR)
                .orderByDesc(PurchaseContracts::getCreateTime)));
    }

    @GetMapping("/getFrameContractsList")
    @ApiOperation(value = "获取框架合同列表", response = ResultBody.class)
    public ResultBody getFrameContractsList() {
        return ResultBody.success(purchaseContractsService.list(Wrappers.<PurchaseContracts>lambdaQuery()
                .select(PurchaseContracts::getId,
                        PurchaseContracts::getContractName,
                        PurchaseContracts::getContractType,
                        PurchaseContracts::getSupplierId,
                        PurchaseContracts::getSupplierName)
                .eq(PurchaseContracts::getApproveStatus,CommonConstant.TWO_STR)
                .eq(PurchaseContracts::getDeleteStatus, CommonConstant.ONE_STR)
                .eq(PurchaseContracts::getContractType,CommonConstant.TWO_STR)
                .orderByDesc(PurchaseContracts::getCreateTime)));
    }
}
